package csc.daonjpa.java.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Component;

import csc.daonjpa.java.controller.EntityManagerFactoryUtil;
import csc.daonjpa.java.domain.Account;
import csc.daonjpa.java.domain.Customer;
/**
 * DAO class for Customer2 entity. This class contains all methods that inserts/updates/deletes
 * account info
 * 
 * @author vuhuynh
 *
 */
@Component
public class CustomerDAO {

	public boolean checkLogin(String id, String pass) {

		// Obtains entity manager object
		EntityManager entityManager = EntityManagerFactoryUtil
				.createEntityManager();

		// Obtains transaction from entity manager
		EntityTransaction entr = entityManager.getTransaction();

		// -----------Begin transaction-----------
		List<Account> accounts = null;
		boolean check = false;
		try {
			entr.begin();
			// Get a list of accounts from DB

			TypedQuery<Customer> query = entityManager
					.createQuery(
							"SELECT c FROM "
									+ Customer.class.getName()
									+ " c Where c.loginId= :loginId and c.password= :password",
							Customer.class);
			query.setParameter("loginId", id);
			query.setParameter("password", pass);
			check = query.getResultList().size() > 0;
			entr.commit();
		} catch (Exception e) {
			entityManager.close();
		}
		// -----------End transaction-----------

		return check;

	}

	public boolean checkUpdateProfile(String id, String fName, String lName,
			String add, String phone) {

		// Obtains entity manager object
		EntityManager entityManager = EntityManagerFactoryUtil
				.createEntityManager();

		// Obtains transaction from entity manager
		EntityTransaction entr = entityManager.getTransaction();

		// -----------Begin transaction-----------

		boolean check = false;
		int updateCount = 0;
		try {
			entr.begin();

			String sqlQuery = "UPDATE "
					+ Customer.class.getName()
					+ " c SET c.firstName= :fName, c.lastName= :lName, c.address= :add, c.phone= :phone WHERE c.loginId= :loginId";
			Query query = entityManager.createQuery(sqlQuery);
			query.setParameter("fName", fName);
			query.setParameter("lName", lName);
			query.setParameter("add", add);
			query.setParameter("phone", phone);
			query.setParameter("loginId", id);

			updateCount = query.executeUpdate();
			if (updateCount > 0) {
				check = true;
			}
			entr.commit();
		} catch (Exception e) {
			System.out.println(" " + e.getMessage());
			entityManager.close();
		}
		// -----------End transaction-----------

		return check;

	}
	public Customer getCustomerByLoginId(String id) {

		// Obtains entity manager object
		EntityManager entityManager = EntityManagerFactoryUtil
				.createEntityManager();

		// Obtains transaction from entity manager
		EntityTransaction entr = entityManager.getTransaction();

		// -----------Begin transaction-----------
		Customer cus = null;
		try {
			entr.begin();
			// Get a list of accounts from DB

			TypedQuery<Customer> query = entityManager.createQuery(
					"SELECT c FROM " + Customer.class.getName()
							+ " c Where c.loginId= :loginId", Customer.class);
			query.setParameter("loginId", id);

			cus = query.getSingleResult();

			entr.commit();
		} catch (Exception e) {
			entityManager.close();
		}
		// -----------End transaction-----------

		return cus;

	}
	public List<Customer> getCustomerProfileByLoginId(String id) {

		// Obtains entity manager object
		EntityManager entityManager = EntityManagerFactoryUtil
				.createEntityManager();

		// Obtains transaction from entity manager
		EntityTransaction entr = entityManager.getTransaction();

		// -----------Begin transaction-----------
		List<Customer> cus = null;
		try {
			entr.begin();
			// Get a list of accounts from DB

			TypedQuery<Customer> query = entityManager.createQuery(
					"SELECT c FROM " + Customer.class.getName()
							+ " c Where c.loginId= :loginId", Customer.class);
			query.setParameter("loginId", id);

			cus = query.getResultList();

			entr.commit();
		} catch (Exception e) {
			entityManager.close();
		}
		// -----------End transaction-----------

		return cus;

	}
	
	public boolean checkUpdatePassword(String id, String pass) {

		// Obtains entity manager object
		EntityManager entityManager = EntityManagerFactoryUtil
				.createEntityManager();

		// Obtains transaction from entity manager
		EntityTransaction entr = entityManager.getTransaction();

		// -----------Begin transaction-----------

		boolean check = false;
		int updateCount = 0;

		try {
			entr.begin();

			String sqlQuery = "UPDATE "
					+ Customer.class.getName()
					+ " c SET c.password= :pass WHERE c.loginId= :loginId";
			Query query = entityManager.createQuery(sqlQuery);
			query.setParameter("pass", pass);
			query.setParameter("loginId", id);

			updateCount = query.executeUpdate();
			if (updateCount > 0) {
				check = true;
			}
			entr.commit();
		} catch (Exception e) {
			System.out.println(" " + e.getMessage());
			entityManager.close();
		}
		// -----------End transaction-----------

		return check;

	}

}
